package cn.modoumama.common.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.core.interfaces.impl.DefaultNameGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.modoumama.common.request.ListRequest;
import cn.modoumama.page.Page;

public class PageUtils {
	protected Logger logger = LoggerFactory.getLogger(PageUtils.class);	
	/**
	 *
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	protected Page getPage(HttpServletRequest request){
		return getPage(request,null);
	}

	/**
	 * 按条件筛选，当orderByClause传入规则时，强制使用该规则，会过滤掉request的排序<br>
	 * <br>  
	 * 创建人：邓强   <br>
	 * 创建时间：2017年12月5日 上午11:49:57    <br> 
	 * 修改人：  <br>
	 * 修改时间：2017年12月5日 上午11:49:57   <br>  
	 * 修改备注：     <br> 
	 * @param request
	 * @param orderByClause
	 * @return
	 */
	protected Page getPage(HttpServletRequest request, String orderByClause){
		Page pageObject = new Page();
		String currPageStr = request.getParameter("page");
		String pageSizeStr = request.getParameter("rows");
		String property = request.getParameter("sort");
		if (StringUtils.isEmpty(orderByClause)) {
			if (!StringUtils.isEmpty(property)) {
				orderByClause = getColumn(property) + " "
						+ request.getParameter("order");
			}
		}
		try{
			pageObject.setCurrPage(Integer.valueOf(currPageStr));
			pageObject.setPageSize(Integer.valueOf(pageSizeStr));
		}catch(NumberFormatException e){
			pageObject.setCurrPage(1);
			pageObject.setPageSize(Integer.MAX_VALUE);
		}
		pageObject.addCondition("orderByClause", orderByClause);
		return pageObject;
	}
	
	/**
	 * 按条件筛选，当没有request里没有排序时，使用默认排序<br>
	 * <br>  
	 * 创建人：邓强   <br>
	 * 创建时间：2017年12月5日 上午11:48:49    <br> 
	 * 修改人：  <br>
	 * 修改时间：2017年12月5日 上午11:48:49   <br>  
	 * 修改备注：     <br> 
	 * @param request
	 * @param orderByClause
	 * @return
	 */
	protected Page getOrderPage(HttpServletRequest request, String orderByClause){
		if(StringUtils.isBlank(orderByClause)){
			throw new RuntimeException("必须传入排序规则");
		}
		
		Page pageObject = new Page();
		String currPageStr = request.getParameter("page");
		String pageSizeStr = request.getParameter("rows");
		String property = request.getParameter("sort");
		if (StringUtils.isNotBlank(property)) {
			orderByClause = getColumn(property) + " " + request.getParameter("order");
		}
		try{
			pageObject.setCurrPage(Integer.valueOf(currPageStr));
			pageObject.setPageSize(Integer.valueOf(pageSizeStr));
		}catch(NumberFormatException e){
			pageObject.setCurrPage(1);
			pageObject.setPageSize(Integer.MAX_VALUE);
		}
		pageObject.addCondition("orderByClause", orderByClause);
		return pageObject;
	}

	private String getColumn(String property) {
		StringBuffer buffer = new StringBuffer(property);
		for (int i = 0; i < buffer.length(); i++) {
			char c = buffer.charAt(i);
			if(Character.isUpperCase(c)){
				buffer.insert(i++, '_');
			}
		}
		return buffer.toString();
	}
	
	public static Page getPage(ListRequest request,String orderByClause){
		Page page = new Page();
		Integer currPageStr = request.getPage();
		Integer pageSizeStr = request.getRows();
		String property = request.getSort();
		DefaultNameGenerator nameGenerator = new DefaultNameGenerator();
		if (!StringUtils.isBlank(property)) {
			orderByClause = nameGenerator.getGeneratorNameByName(property) + " " + request.getOrder();
		}

		if(currPageStr != null){
			page.setCurrPage(currPageStr);
		}
		if(currPageStr != null){
			page.setPageSize(pageSizeStr);
		}
		page.addCondition("orderByClause", orderByClause);
		return page;
	}

}
